public class Test1 {
    public static void main(String[] args) {
      ListNode node1 = new ListNode(1) ;
        ListNode node2 = new ListNode(2) ;
        ListNode node3 = new ListNode(3) ;
        ListNode node4 = new ListNode(4) ;
        ListNode node5 = new ListNode(5) ;
        node1.next = node2 ;
        node2.next =node3 ;
        node3.next = node4 ;
        node4.next = node5 ;
        ListNode head = reverseList(node1) ;
        display(head);


    }
    public static ListNode reverseList(ListNode head) {
        if(head == null) return null;
        ListNode cur = head.next ;
        while(cur != null){
            ListNode prev = head ;
            prev.next = cur.next ;
            cur.next = prev ;
            head = cur ;
            cur = prev.next ;
        }
        return head ;
    }
    public static void display(ListNode head){
        ListNode ret = head ;
        while(ret != null){
            System.out.print(ret.val + "  ");
            ret = ret.next ;
        }
        System.out.println();
    }

}
class ListNode {
      int val;
      ListNode next;
      ListNode() {}
      ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  }